package de.bazinga.rotw.network;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;

import com.captiveimagination.jgn.JGN;
import com.captiveimagination.jgn.clientserver.JGNServer;
import com.captiveimagination.jgn.synchronization.SynchronizationManager;
import com.captiveimagination.jmenet.JMEGraphicalController;
import com.jmex.game.state.GameStateManager;

import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.network.message.RegisterMessages;
import de.bazinga.rotw.network.message.ScoreUpdateMessage;
import de.bazinga.rotw.network.message.SynchronizeCreatePlayerMessage;
import de.bazinga.rotw.person.OpponentList;
import de.bazinga.rotw.person.Person;
import de.bazinga.rotw.statistics.StatisticState;
import de.bazinga.rotw.util.NetworkConstants;

public class Server extends BasicSyncManager {

	private JGNServer server = null;

	private SynchronizationManager syncManager = null;
	
	public Server(RotwGameState gameServer) throws IOException {
		// Register Messages
		RegisterMessages.registerMessages();
		
        // Initialize networking
        InetSocketAddress serverReliable = new InetSocketAddress((InetAddress)null, NetworkConstants.SERVER_TCP_PORT);
        InetSocketAddress serverFast = new InetSocketAddress((InetAddress)null, NetworkConstants.SERVER_UDP_PORT);
        server = new JGNServer(serverReliable, serverFast);
        
        //Instantiate an instance of a JMEGraphicalController
        JMEGraphicalController controller = new JMEGraphicalController();
        
        
        // Create SynchronizationManager instance for this server
        syncManager = new SynchronizationManager(server, controller);
        syncManager.addSyncObjectManager(this);
 
        JGN.createThread(server).start();
 
        JGN.createThread(syncManager).start();

        server.addClientConnectionListener(new RotwConnectionListener(gameServer));
        
        server.addMessageListener(new ServerMessageListener());
        
		this.setScene(gameServer.getRootNode());
		
		SynchronizeCreatePlayerMessage scm = new SynchronizeCreatePlayerMessage();
        scm.setSyncObjectType(NetworkConstants.OBJ_PLAYER);
        scm.setPlayerName(gameServer.getPlayer().getPlayerName());
        scm.setModelTexture(gameServer.getPlayer().getTextureColor());
        scm.setTeam(Person.TEAM_ONE);
        syncManager.register(gameServer.getPlayer(), scm, NetworkConstants.UPDATE_INTERVALL);
        
        
        updateScore();
        
        
        // Send online-information
        ServerList.sendServerUpdate(gameServer.getServerName());
        gameServer.setLastUpdateSend(System.currentTimeMillis());
        

        
        // INit GameMode
        gameServer.getGameMode().initServer(server);
	}
	
	public void close() {
		try {
			server.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
	}

	
	
	
	
	public void updateScore() {
		ScoreUpdateMessage updateMsg = new ScoreUpdateMessage();
		updateMsg.setNewScores(OpponentList.getInstance().getScoreList());
		server.sendToAll(updateMsg);
		
		((StatisticState)GameStateManager.getInstance().getChild("statistics")).setScores(
				OpponentList.getInstance().getScoreList());
    	
	}
	
	public SynchronizationManager getSyncManager() {
		return syncManager;
	}
	
	public JGNServer getServer() {
		return server;
	}
}